package com.example.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.management.common.QueryPageParam;
import com.example.management.common.Result;
import com.example.management.entity.Employee;
import com.example.management.entity.Product;
import com.example.management.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    //查询
    @GetMapping("/list")
    public List<Product> list() { return productService.list(); }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Product product) {
        return productService.save(product) ? Result.success() : Result.fail();
    }
    @PostMapping("/update")
    public Result update(@RequestBody Product product) {
        return productService.updateById(product) ? Result.success() : Result.fail();
    }
    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody Product product) {
        return productService.updateById(product);
    }
    //新增或修改
    @PostMapping("/saveOrMod")
    public boolean saveOfMod(@RequestBody Product product) {
        return productService.saveOrUpdate(product);
    }
    //删除
    @GetMapping("/delete")
    public Result delete(String pno) {
        return productService.removeById(pno) ? Result.success() : Result.fail();
    }
    @GetMapping("/findByVno")
    public Result findByVno(@RequestParam String vno) {
        List<Product> list = productService.lambdaQuery().eq(Product::getVno, vno).list();
        return list.isEmpty() ? Result.fail() : Result.success();
    }
    @GetMapping("/findByNo")
    public Result findByNo(@RequestParam String pno) {
        List<Product> list = productService.lambdaQuery().eq(Product::getPno, pno).list();
        return list.isEmpty() ? Result.fail() : Result.success();
    }
    //查询（模糊、匹配）
    @PostMapping("/listP")
    public List<Product> listP(@RequestBody Product product) {
        LambdaQueryWrapper<Product> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Product::getPname, product.getPname());
        return productService.list(lambdaQueryWrapper);
    }
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        System.out.println(query);
        System.out.println("num==" + query.getPageNum());
        System.out.println("size==" + query.getPageSize());
        HashMap param = query.getParam();
        String no = (String) param.get("pno");
        String name = (String) param.get("pname");

        Page<Product> page = new Page<>(query.getPageNum(), query.getPageSize());
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Product> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(no) && !"null".equals(no)) {
            lambdaQueryWrapper.eq(Product::getPno, no);
        }
        if(StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(Product::getPname, name);
        }

        IPage<Product> data = productService.page(page, lambdaQueryWrapper);
        System.out.println("total==" + data.getTotal());

        return Result.success(data.getRecords(), data.getTotal());
    }
}